package com.project;

import com.project.config.WebMvcConfig;
import com.project.filter.*;
import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.Filter;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;

/**
 * 启动类
 */
@WebListener
public class BootShop {
    public BootShop(){
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8088);
        tomcat.getConnector();
        Context ctx = tomcat.addContext("",null);
        //注册监听器--复制监听器的引用
        this.registerFilter(ctx,new CharFilter(),"/*");
        this.registerFilter(ctx,new CustomFilter(),"/html/custom/*");
        this.registerFilter(ctx,new ManagerFilter(),"/html/manager/*");
        this.registerFilter(ctx,new SendFilter(),"/html/send/*");
        this.registerFilter(ctx,new BossFilter(),"/html/boss/*");
//        ctx.addApplicationListener("com.project.listener.ContextListener");
        //session监听器注册
//        ctx.addApplicationListener("com.project.listener.SessionListener");
        //session属性监听器注册
//        ctx.addApplicationListener("com.project.listener.SessionAttributeListener");
        WebApplicationContext wac = createApplicationContext(ctx.getServletContext());
        //创建前端控制器对象
        DispatcherServlet dis = new DispatcherServlet(wac);
        //注册前端控制器
        Wrapper wrp = Tomcat.addServlet(ctx,"dis",dis);
        wrp.setLoadOnStartup(0);//数字越小优先得到加载，不能是负数
        wrp.addMapping("/*");

        try {
            tomcat.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 注册过滤器
     * @param  ctx          应用上下文
     * @param filterObj     注册对象
     * @param urlPattern    注册范围
     */
    public void registerFilter(Context ctx ,Filter filterObj,String urlPattern){
        FilterDef fd = new FilterDef();
        fd.setFilterName(filterObj.getClass().getName());
        fd.setFilter(filterObj);

        FilterMap fm = new FilterMap();
        fm.setFilterName(filterObj.getClass().getName());
        fm.addURLPattern(urlPattern);

        ctx.addFilterDef(fd);
        ctx.addFilterMap(fm);
    }

    /**
     * 创建springMvc应用上下文
     * @param servletContext
     * @return
     */
    public static WebApplicationContext createApplicationContext(
            ServletContext servletContext) {
        AnnotationConfigWebApplicationContext ctx
                = new AnnotationConfigWebApplicationContext();
        ctx.register(WebMvcConfig.class);//配置类名称

        ctx.setServletContext(servletContext);
        ctx.refresh();
        ctx.registerShutdownHook();
        return ctx;
    }


    public static void main(String[] args) {
        new BootShop();
    }

}
